package com.future.lock.me.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.future.baselib.activity.BaseActivity;
import com.future.lock.R;
import com.future.lock.common.MyApp;
import com.future.lock.common.RefreshDataUtils;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.find.NewsDetailActivity;
import com.future.lock.find.adapter.FindAdapter;
import com.future.lock.find.bean.FindListBean;
import com.future.lock.me.entity.response.FindListResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyFavoriteActivity extends BaseActivity {

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private FindAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_my_favorite);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        setTitle("我的收藏");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new FindAdapter(null);
        recyclerView.setAdapter(adapter);
        View emptyView = getLayoutInflater().inflate(R.layout.layout_default_empty, null);
        ((TextView) emptyView.findViewById(R.id.tv_tib)).setText("暂无收藏");
        adapter.setEmptyView(emptyView);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", ((FindListBean) adapter.getData().get(position)).id);
                startActivity(NewsDetailActivity.class, bundle);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        HttpUtils.getHttpApi().getCollectList(MyApp.getUserToken()).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                FindListResponse findListResponse = new FindListResponse();
                findListResponse.parse(response.body());
                if (findListResponse.success == 1) {
                    adapter.setNewData(findListResponse.data);
                }else{
                    if (findListResponse.code == 112){
                        RefreshDataUtils.logout(mContext);
                    }
                    toast(findListResponse.msg);
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                toast(t.getMessage());
            }
        });
    }

    @Override
    protected void initLogic() {

    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }
}
